﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

namespace ProblemsSet
{
    public class Problem_139 : BaseProblem
    {
        public override object GetResult()
        {

            const int max = 100000000;

            var mx = (long)Math.Sqrt(max/(double)2);

            long res = 0;
                for (long a = 2; a <= mx; a++)
                {
                    var mmx = Math.Min(max/(double) (2*a) - a,a);
                    for (long b = 1; b < mmx; b++)
                    {
                            var x = (a + b)*(a - b);
                            var y = 2*a*b;
                            if (MathLogic.GetNOD(x, y) != 1) continue;
                            var z = a*a + b*b;
                            if (x + y + z >= max|| x+y+z < 0)
                                break;
                            var q = Math.Abs(x - y);
                            if (z%q == 0)
                            {
                                res += max/(x + y + z);
                            }
                    }
                }
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"Let (a, b, c) represent the three sides of a right angle triangle with integral length sides. It is possible to place four such triangles together to form a square with length c.

For example, (3, 4, 5) triangles can be placed together to form a 5 by 5 square with a 1 by 1 hole in the middle and it can be seen that the 5 by 5 square can be tiled with twenty-five 1 by 1 squares.


However, if (5, 12, 13) triangles were used then the hole would measure 7 by 7 and these could not be used to tile the 13 by 13 square.

Given that the perimeter of the right triangle is less than one-hundred million, how many Pythagorean triangles would allow such a tiling to take place?";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 10057761;
            }
        }
    }
}
